package com.miracle9.game.service;

import com.miracle9.game.dao.*;
import com.miracle9.game.entity.*;
import com.miracle9.game.enums.GameTypeEnums;
import com.miracle9.game.server.message.AmfMessageSend;
import com.miracle9.game.server.message.*;
import com.miracle9.game.util.GameParameterAssist;
import com.miracle9.game.util.MyUtil;
import com.miracle9.game.util.SessionUtils;
import com.miracle9.game.util.UserParticipateRecord;
import flex.messaging.io.ArrayCollection;
import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service("gameService")
public class GameService {

    private static final Logger log = Logger.getLogger(GameService.class);
    public static final int LUCK = 0;
    public static final int FISH = 1;
    public static final int CARD = 2;
    public static final int BULLET = 3;
    public static final int MERMAID = 4;
    public static final int LACK = 5;
    public static final int JOY = 6;
    public static final int Water = 7;
    @Autowired
    private SystemConfigServiceDao systemConfigServiceDao;
    @Autowired
    private CardDeskServiceDao cardDeskServiceDao;
    @Autowired
    private LackDeskServiceDao lackDeskServiceDao;
    @Autowired
    private JoyDeskServiceDao joyDeskServiceDao;
    @Autowired
    private WaterDeskServiceDao waterDeskServiceDao;
    @Autowired
    private DeskServiceDao deskServiceDao;
    @Autowired
    private FishDeskServiceDao fishDeskServiceDao;
    @Autowired
    private FishKingDB fishKingDB;
    @Autowired
    private com.miracle9.game.dao.BulletFishDeskServiceDao bulletFishDeskServiceDao;
    @Autowired
    private MermaidDeskServiceDao mermaidDeskServiceDao;
    @Autowired
    private UserTopServiceDao userTopServiceDao;
    @Autowired
    private UserServiceDao userServiceDao;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private DeskService deskService;
    @Autowired
    private com.miracle9.game.dao.ActivityInfoServiceDao activityInfoServiceDao;
    @Autowired
    private com.miracle9.game.dao.AdminLogServiceDao adminLogDao;

    private static String getGameName(int gameType) {
        GameTypeEnums gameTypeEnums = GameTypeEnums.getByType(gameType);
        if (gameTypeEnums != null) {
            return "<" + gameTypeEnums.getName() + ">";
        }
        return "<未知>";
    }

    public void login(int userId, int type, IoSession session) {
        if (GameParameterAssist.aB) {
            if ((type == GameTypeEnums.GT_0.getType()) && (GameParameterAssist.bh.containsKey(userId))) {
                return;
            }
            if ((type == GameTypeEnums.GT_1.getType()) && (GameParameterAssist.bi.containsKey(userId))) {
                return;
            }
            if ((type == GameTypeEnums.GT_2.getType()) && (GameParameterAssist.bj.containsKey(userId))) {
                return;
            }
            if ((type == GameTypeEnums.GT_3.getType()) && (GameParameterAssist.bk.containsKey(userId))) {
                return;
            }
            if ((type == GameTypeEnums.GT_4.getType()) && (GameParameterAssist.bl.containsKey(userId))) {
                return;
            }
            if ((type == GameTypeEnums.GT_5.getType()) && (GameParameterAssist.aN.containsKey(userId))) {
                return;
            }
            if ((type == GameTypeEnums.GT_6.getType()) && (GameParameterAssist.aP.containsKey(userId))) {
                return;
            }
            if ((type == GameTypeEnums.GT_7.getType()) && (GameParameterAssist.bs.containsKey(userId))) {
                return;
            }
            if ((type == GameTypeEnums.GT_9.getType()) && (GameParameterAssist.thousandFishGametypeMap.containsKey(userId))) {
                return;
            }
            if ((type == GameTypeEnums.GT_11.getType()) && (GameParameterAssist.fishKingBi.containsKey(userId))) {
                return;
            }
            if ((type == GameTypeEnums.GT_20.getType()) && (GameParameterAssist.likuiFishUserId.containsKey(userId))) {
                return;
            }
            if ((type == GameTypeEnums.GT_37.getType()) && (GameParameterAssist.goldenFishUserId.containsKey(userId))) {
                return;
            }
            if ((type == GameTypeEnums.GT_92.getType()) && (GameParameterAssist.tgUserId.containsKey(userId))) {
                return;
            }
            if ((type == GameTypeEnums.GT_42.getType()) && (GameParameterAssist.goldSharkUserId.containsKey(userId))) {
                return;
            }
            if ((type == GameTypeEnums.GT_52.getType()) && (GameParameterAssist.bcbmUserId.containsKey(userId))) {
                return;
            }
            if ((type == GameTypeEnums.GT_103.getType()) && (GameParameterAssist.lhdUserId.containsKey(userId))) {
                return;
            }
            int nGameType = 0;
            synchronized (this) {
                nGameType = UserParticipateRecord.checkUserId_1(userId);
                log.info("login now userId:" + userId + "is " + getGameName(nGameType));
                UserParticipateRecord.updateStatus(userId, type);
                log.info("login now userId:" + userId + " change to " + getGameName(nGameType));
            }
            if (nGameType == -1) {
                UserParticipateRecord.updateStatus(userId, -1);
            }
            gameKick(nGameType, userId);
        }
        if (type == GameTypeEnums.GT_0.getType()) {
            GameParameterAssist.bh.put(userId, type);
        } else if (type == GameTypeEnums.GT_1.getType()) {
            GameParameterAssist.bi.put(userId, type);
        } else if (type == GameTypeEnums.GT_2.getType()) {
            GameParameterAssist.bj.put(userId, type);
        } else if (type == GameTypeEnums.GT_3.getType()) {
            GameParameterAssist.bk.put(userId, type);
        } else if (type == GameTypeEnums.GT_4.getType()) {
            GameParameterAssist.bl.put(userId, type);
        } else if (type == GameTypeEnums.GT_5.getType()) {
            GameParameterAssist.aN.put(userId, type);
        } else if (type == GameTypeEnums.GT_6.getType()) {
            GameParameterAssist.aP.put(userId, type);
        } else if (type == GameTypeEnums.GT_7.getType()) {
            GameParameterAssist.bs.put(userId, type);
        } else if (type == GameTypeEnums.GT_9.getType()) {
            GameParameterAssist.thousandFishGametypeMap.put(userId, type);
        } else if (type == GameTypeEnums.GT_11.getType()) {
            GameParameterAssist.fishKingBi.put(userId, type);
        } else if (type == GameTypeEnums.GT_20.getType()) {
            GameParameterAssist.likuiFishUserId.put(userId, type);
        } else if (type == GameTypeEnums.GT_37.getType()) {
            GameParameterAssist.goldenFishUserId.put(userId, type);
        } else if (type == GameTypeEnums.GT_42.getType()) {
            GameParameterAssist.goldSharkUserId.put(userId, type);
        }  else if (type == GameTypeEnums.GT_52.getType()) {
            GameParameterAssist.bcbmUserId.put(userId, type);
        } else if (type == GameTypeEnums.GT_92.getType()) {
            GameParameterAssist.tgUserId.put(userId, type);
        } else if (type == GameTypeEnums.GT_103.getType()) {
            GameParameterAssist.lhdUserId.put(userId, type);
        }
        User user = (User) this.userServiceDao.getById(userId);
        if ((user.getPromoterId() == 0) && (user.getType() == 0)) {
            if (type == GameTypeEnums.GT_0.getType()) {
                GameParameterAssist.aS.put(userId, type);
            } else if (type == GameTypeEnums.GT_1.getType()) {
                GameParameterAssist.aT.put(userId, type);
            } else if (type == GameTypeEnums.GT_2.getType()) {
                GameParameterAssist.aU.put(userId, type);
            } else if (type == GameTypeEnums.GT_3.getType()) {
                GameParameterAssist.aV.put(userId, type);
            } else if (type == GameTypeEnums.GT_4.getType()) {
                GameParameterAssist.aW.put(userId, type);
            } else if (type == GameTypeEnums.GT_5.getType()) {
                GameParameterAssist.aX.put(userId, type);
            } else if (type == GameTypeEnums.GT_6.getType()) {
                GameParameterAssist.aY.put(userId, type);
            } else if (type == GameTypeEnums.GT_7.getType()) {
                GameParameterAssist.aZ.put(userId, type);
            } else if (type == GameTypeEnums.GT_9.getType()) {
                GameParameterAssist.thounsandFishGameType2.put(userId, type);
            } else if (type == GameTypeEnums.GT_11.getType()) {
                GameParameterAssist.aT_king.put(userId, type);
            } else if (type == GameTypeEnums.GT_20.getType()) {
                GameParameterAssist.aT_likui.put(userId, type);
            } else if (type == GameTypeEnums.GT_37.getType()) {
                GameParameterAssist.aT_golden.put(userId, type);
            } else if (type == GameTypeEnums.GT_42.getType()) {
                GameParameterAssist.aT_goldShark.put(userId, type);
            }  else if (type == GameTypeEnums.GT_52.getType()) {
                GameParameterAssist.aT_bcbm.put(userId, type);
            } else if (type == GameTypeEnums.GT_92.getType()) {
                GameParameterAssist.aT_tg.put(userId, type);
            } else if (type == GameTypeEnums.GT_103.getType()) {
                GameParameterAssist.aT_lhd.put(userId, type);
            }
        }
        log.info("1*********login:" + user.getUsername() + "," + type + "," + user.getGameGold() + "," + user.getGameScore());
        log.info("FORWATER:gameService login:userId:" + userId);
        synchronized (this) {
            log.info("login from game:" + getGameName(type) + ",userId:" + userId);
            int loginStatus = UserParticipateRecord.checkUserId_3(userId);
            log.info("loginStatus before:" + getGameName(loginStatus));
            if (loginStatus == -3) {
                UserParticipateRecord.addUserStatusMapCP(userId, type);
            } else {
                if (loginStatus == type) {
                    gameKick(loginStatus, userId);
                    log.info("loginStatus wrong kick userId:" + userId);

                    return;
                }
                log.info("loginStatus wrong begin kick two in game userId:" + userId);
                log.error("loginStatus wrong begin kick two in game userId:" + userId);
                this.userServiceDao.updateStatus(userId, 3);
                AdminLog adminLog = new AdminLog("系统防刷分管理员", "自动对会员“" + user.getUsername() + "”进行了“封号”操作" + ",原因：在不同的小游戏同时登录", 0, 0, "Frozen the member which ID is " + user.getUsername());
                this.adminLogDao.addAdminLog(adminLog);
                gameKick(loginStatus, userId);
                gameKick(type, userId);
                UserService.quitToLogin(userId, 2);
                log.info("kick and freez success");
                com.miracle9.game.thread.DealCheatThread.f(userId);
            }
        }
    }

    public static void gameKick(int nGameType, int userId) {
        log.info("gameKick now kick userId " + userId + " in game " + nGameType);
        Set<IoSession> sessions = SessionUtils.getSessionByGameType(nGameType);
        for (IoSession session : sessions) {
            AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(session, "manageService/quitToLogin", new Object[]{userId, 4}));
        }
    }

    public void logout(int userId, int type, IoSession session) {
        log.info("logout userId " + userId + " in game " + type);
        if (UserParticipateRecord.checkUserId_1(userId) != 100) {
            UserParticipateRecord.updateStatus(userId, -1);
        }
        UserParticipateRecord.removeUserIdMapCQ(userId);
        UserParticipateRecord.removeUserIdMapCR(userId);
        UserParticipateRecord.removeUserIdMapCP(userId);
        if (type == GameTypeEnums.GT_0.getType()) {
            GameParameterAssist.bh.remove(userId);
        } else if (type == GameTypeEnums.GT_1.getType()) {
            GameParameterAssist.bi.remove(userId);
        } else if (type == GameTypeEnums.GT_2.getType()) {
            GameParameterAssist.bj.remove(userId);
        } else if (type == GameTypeEnums.GT_3.getType()) {
            GameParameterAssist.bk.remove(userId);
        } else if (type == GameTypeEnums.GT_4.getType()) {
            GameParameterAssist.bl.remove(userId);
        } else if (type == GameTypeEnums.GT_5.getType()) {
            GameParameterAssist.aN.remove(userId);
        } else if (type == GameTypeEnums.GT_6.getType()) {
            GameParameterAssist.aP.remove(userId);
        } else if (type == GameTypeEnums.GT_7.getType()) {
            GameParameterAssist.bs.remove(userId);
        } else if (type == GameTypeEnums.GT_9.getType()) {
            GameParameterAssist.thousandFishGametypeMap.remove(userId);
        } else if (type == GameTypeEnums.GT_11.getType()) {
            GameParameterAssist.fishKingBi.remove(userId);
        } else if (type == GameTypeEnums.GT_20.getType()) {
            GameParameterAssist.likuiFishUserId.remove(userId);
        } else if (type == GameTypeEnums.GT_37.getType()) {
            GameParameterAssist.goldenFishUserId.remove(userId);
        } else if (type == GameTypeEnums.GT_42.getType()) {
            GameParameterAssist.goldSharkUserId.remove(userId);
        }  else if (type == GameTypeEnums.GT_52.getType()) {
            GameParameterAssist.bcbmUserId.remove(userId);
        } else if (type == GameTypeEnums.GT_92.getType()) {
            GameParameterAssist.tgUserId.remove(userId);
        } else if (type == GameTypeEnums.GT_103.getType()) {
            GameParameterAssist.lhdUserId.remove(userId);
        }
        User user = (User) this.userServiceDao.getById(userId);
        if ((user.getPromoterId() == 0) && (user.getType() == 0)) {
            if (type == GameTypeEnums.GT_0.getType()) {
                GameParameterAssist.aS.remove(userId);
            } else if (type == GameTypeEnums.GT_1.getType()) {
                GameParameterAssist.aT.remove(userId);
            } else if (type == GameTypeEnums.GT_2.getType()) {
                GameParameterAssist.aU.remove(userId);
            } else if (type == GameTypeEnums.GT_3.getType()) {
                GameParameterAssist.aV.remove(userId);
            } else if (type == GameTypeEnums.GT_4.getType()) {
                GameParameterAssist.aW.remove(userId);
            } else if (type == GameTypeEnums.GT_5.getType()) {
                GameParameterAssist.aX.remove(userId);
            } else if (type == GameTypeEnums.GT_6.getType()) {
                GameParameterAssist.aY.remove(userId);
            } else if (type == GameTypeEnums.GT_7.getType()) {
                GameParameterAssist.aZ.remove(userId);
            } else if (type == GameTypeEnums.GT_9.getType()) {
                GameParameterAssist.thounsandFishGameType2.remove(userId);
            } else if (type == GameTypeEnums.GT_11.getType()) {
                GameParameterAssist.aT_king.remove(userId);
            } else if (type == GameTypeEnums.GT_20.getType()) {
                GameParameterAssist.aT_likui.remove(userId);
            } else if (type == GameTypeEnums.GT_37.getType()) {
                GameParameterAssist.aT_golden.remove(userId);
            } else if (type == GameTypeEnums.GT_42.getType()) {
                GameParameterAssist.aT_goldShark.remove(userId);
            }  else if (type == GameTypeEnums.GT_52.getType()) {
                GameParameterAssist.aT_bcbm.remove(userId);
            } else if (type == GameTypeEnums.GT_92.getType()) {
                GameParameterAssist.aT_tg.remove(userId);
            } else if (type == GameTypeEnums.GT_103.getType()) {
                GameParameterAssist.aT_lhd.remove(userId);
            }
        }
    }

    public void deskInfo(int deskId, ArrayCollection users, int type, IoSession session) {
        if (type == GameTypeEnums.GT_0.getType()) {
            GameParameterAssist.bm.put(deskId, users);
        } else if (type == GameTypeEnums.GT_1.getType()) {
            GameParameterAssist.bn.put(deskId, users);
        } else if (type == GameTypeEnums.GT_2.getType()) {
            GameParameterAssist.bo.put(deskId, users);
        } else if (type == GameTypeEnums.GT_3.getType()) {
            GameParameterAssist.bp.put(deskId, users);
        } else if (type == GameTypeEnums.GT_4.getType()) {
            GameParameterAssist.bq.put(deskId, users);
        } else if (type == GameTypeEnums.GT_5.getType()) {
            GameParameterAssist.aO.put(deskId, users);
        } else if (type == GameTypeEnums.GT_6.getType()) {
            GameParameterAssist.aQ.put(deskId, users);
        } else if (type == GameTypeEnums.GT_7.getType()) {
            GameParameterAssist.br.put(deskId, users);
        } else if (type == GameTypeEnums.GT_9.getType()) {
            GameParameterAssist.thsandFishUserList.put(deskId, users);
        } else if (type == GameTypeEnums.GT_11.getType()) {
            GameParameterAssist.bn_king.put(deskId, users);
        } else if (type == GameTypeEnums.GT_20.getType()) {
            GameParameterAssist.likuiFishUserList.put(deskId, users);
        } else if (type == GameTypeEnums.GT_37.getType()) {
            GameParameterAssist.goldenFishUserList.put(deskId, users);
        } else if (type == GameTypeEnums.GT_42.getType()) {
            GameParameterAssist.goldSharkUserList.put(deskId, users);
        }  else if (type == GameTypeEnums.GT_52.getType()) {
            GameParameterAssist.bcbmUserList.put(deskId, users);
        } else if (type == GameTypeEnums.GT_92.getType()) {
            GameParameterAssist.tgUserList.put(deskId, users);
        } else if (type == GameTypeEnums.GT_103.getType()) {
            GameParameterAssist.lhdUserList.put(deskId, users);
        }
    }

    /*public void waterDeskInfo(ArrayCollection arrDesk, ArrayCollection arrUser, IoSession session) {
    }*/

    public void refreshUserTop(int type, IoSession session) {
        this.userTopServiceDao.voidGetByType(type);
        String game = GameTypeEnums.getByType(type).getName();
        List<UserTop> newTops = this.userTopServiceDao.topList(type, 1);
        if (CollectionUtils.isEmpty(newTops)) {
            return;
        }
        int topId = 0;
        String nickName = "";
        List<UserTop> oldTops = GameParameterAssist.aK.get(type);
        if (CollectionUtils.isEmpty(oldTops)) {
            UserTop userTop = newTops.get(0);
            topId = userTop.getId();
            nickName = userTop.getNickname();
            String content = "天意昭昭，势不可挡！" + userTop.getNickname() + "成功进入" + game + "日排行榜，位列第1名，可喜可贺。";
            sendNotice(content);
        } else {
            for (int i = 0; i < newTops.size(); i++) {
                UserTop userTop = newTops.get(i);
                if (oldTops.size() >= i + 1) {
                    UserTop oldTop = oldTops.get(i);
                    if (userTop.getId() != oldTop.getId()) {
                        topId = userTop.getId();
                        nickName = userTop.getNickname();
                        String content = "天意昭昭，势不可挡！" + userTop.getNickname() + "成功进入" + game + "日排行榜，位列第" + (i + 1) + "名，可喜可贺。";
                        sendNotice(content);
                        break;
                    }
                } else {
                    topId = userTop.getId();
                    nickName = userTop.getNickname();
                    String content = "天意昭昭，势不可挡！" + userTop.getNickname() + "成功进入" + game + "日排行榜，位列第" + (i + 1) + "名，可喜可贺。";
                    sendNotice(content);
                }
            }
        }
        GameParameterAssist.aK.put(type, newTops);
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_WEEK, 2);
        String datetime = MyUtil.dateFormatYMD_Sting(c.getTime());
        int weekTopNum = this.userTopServiceDao.q_CreateSqlInt("select count(*) from UserTop where type=? and datetime >=? and id=?", new Object[]{type, datetime, topId});
        if (weekTopNum > 0) {
            Query query = this.userTopServiceDao.q_CreateSql("select count(*) from UserTop where type=? and datetime>=? and gold>(select max(gold) from UserTop where type=? and datetime>=? and id=?)", new Object[]{type, datetime, type, datetime, topId});
            Object temp = query.uniqueResult();
            weekTopNum = Integer.parseInt(temp.toString()) + 1;
            if (weekTopNum <= 10) {
                String content = "天意昭昭，势不可挡！" + nickName + "成功进入" + game + "周排行榜，位列第" + weekTopNum + "名，可喜可贺。";
                sendNotice(content);
            }
        }
        int allTopNum = this.userTopServiceDao.q_CreateSqlInt("select count(*) from UserTop where type=? and id=?", new Object[]{type, topId});
        if (allTopNum > 0) {
            Query query = this.userTopServiceDao.q_CreateSql("select count(*) from UserTop where type=? and gold>(select max(gold) from UserTop where type=? and id=?)", new Object[]{type, type, topId});
            Object temp = query.uniqueResult();
            allTopNum = Integer.parseInt(temp.toString()) + 1;
            if (allTopNum <= 10) {
                String content = "天意昭昭，势不可挡！" + nickName + "成功进入" + game + "总排行榜，位列第" + allTopNum + "名，可喜可贺。";
                sendNotice(content);
            }
        }
    }

    public void refreshUserTop2(int type, IoSession session) {
        log.info("refreshUserTop type：" + type);
        this.userTopServiceDao.voidGetByType(type);
        String nickName = "";
        String game = "";
        int honor = 0;
        String strType = "";
        String content = "";
        int topId = 0;
        UserTop userTop = new UserTop();
        if (type == 0) {
            game = "幸运六狮";
        } else if (type == 1) {
            game = "摇钱树";
        } else if (type == 2) {
            game = "单挑";
        } else if (type == 3) {
            game = "牛魔王";
        } else if (type == 4) {
            game = "QQ美人鱼";
        } else if (type == 5) {
            game = "缺一门";
        } else if (type == 6) {
            game = "欢乐牛牛";
        } else if (type == 7) {
            game = "水浒传";
        }
        List<UserTop> newTops = this.userTopServiceDao.topList(type, 1);
        List<UserTop> oldTops = GameParameterAssist.aK.get(type);
        if ((oldTops.size() == 0) && (newTops.size() != 0)) {
            oldTops = GameParameterAssist.aK.put(type, newTops);
            userTop = newTops.get(0);
            nickName = userTop.getNickname();
            honor = newTops.indexOf(userTop) + 1;
            topId = userTop.getId();
            strType = "日";
            content = "天意昭昭，势不可挡！" + nickName + "成功进入" + game + " " + strType + "排行榜，位列第" + honor + "名，可喜可贺。";
            sendNotice(content);
        } else if (oldTops.size() > 0) {
            for (int i = 0; i < newTops.size(); i++) {
                UserTop uNew = (UserTop) newTops.get(i);
                if (oldTops.size() >= i + 1) {
                    UserTop uOld = (UserTop) oldTops.get(i);
                    if (uNew.getId() != uOld.getId()) {
                        oldTops = GameParameterAssist.aK.put(type, newTops);
                        nickName = uNew.getNickname();
                        honor = newTops.indexOf(uNew) + 1;
                        topId = uNew.getId();
                        strType = "日";
                        content = "天意昭昭，势不可挡！" + nickName + "成功进入" + game + " " + strType + "排行榜，位列第" + honor + "名，可喜可贺。";
                        sendNotice(content);
                        break;
                    }
                } else {
                    oldTops = GameParameterAssist.aK.put(type, newTops);
                    nickName = uNew.getNickname();
                    honor = newTops.indexOf(uNew) + 1;
                    topId = uNew.getId();
                    strType = "日";
                    content = "天意昭昭，势不可挡！" + nickName + "成功进入" + game + " " + strType + "排行榜，位列第" + honor + "名，可喜可贺。";
                    sendNotice(content);
                }
            }
        }
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_WEEK, 2);
        String datetime = MyUtil.dateFormatYMD_Sting(c.getTime());
        int weekTopNum = this.userTopServiceDao.q_CreateSqlInt("select count(*) from UserTop where type=? and datetime >=? and id=?", new Object[]{type, datetime, Integer.valueOf(topId)});
        if (weekTopNum > 0) {
            String hql = "select count(*) from UserTop where type=? and datetime>=? and gold>(select max(gold) from UserTop where type=? and datetime>=? and id=?)";
            Query query = this.userTopServiceDao.q_CreateSql("select count(*) from UserTop where type=? and datetime>=? and gold>(select max(gold) from UserTop where type=? and datetime>=? and id=?)", new Object[]{Integer.valueOf(type), datetime, Integer.valueOf(type), datetime, Integer.valueOf(topId)});
            Object temp = query.uniqueResult();
            weekTopNum = Integer.parseInt(temp.toString()) + 1;
            if (weekTopNum <= 10) {
                honor = weekTopNum;
                strType = "周";
                content = "天意昭昭，势不可挡！" + nickName + "成功进入" + game + " " + strType + "排行榜，位列第" + honor + "名，可喜可贺。";
                sendNotice(content);
            }
        }
        int allTopNum = this.userTopServiceDao.q_CreateSqlInt("select count(*) from UserTop where type=? and id=?", new Object[]{Integer.valueOf(type), Integer.valueOf(topId)});
        if (allTopNum > 0) {
            String hql2 = "select count(*) from UserTop where type=? and gold>(select max(gold) from UserTop where type=? and id=?)";
            Query query2 = this.userTopServiceDao.q_CreateSql("select count(*) from UserTop where type=? and gold>(select max(gold) from UserTop where type=? and id=?)", new Object[]{Integer.valueOf(type), Integer.valueOf(type), Integer.valueOf(topId)});
            Object temp2 = query2.uniqueResult();
            allTopNum = Integer.parseInt(temp2.toString()) + 1;
            if (allTopNum <= 10) {
                honor = allTopNum;
                strType = "总";
                content = "天意昭昭，势不可挡！" + nickName + "成功进入" + game + " " + strType + "排行榜，位列第" + honor + "名，可喜可贺。";
                sendNotice(content);
            }
        }
    }

    public void printState(boolean state, IoSession session) {
        SystemConfig config = this.systemConfigServiceDao.getSystemConfig();
        if (CardSocketHandler.w.contains(session)) {
            if (config.getBaodanStatus() != -1) {
                int status = state ? 1 : 0;
                config.setBaodanStatus(status);
                this.systemConfigServiceDao.updateLackBaodanStatus(status);
            }
            for (Map.Entry entry : GameParameterAssist.be.entrySet()) {
                AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity((IoSession) entry.getValue(), "syncConfig", new Object[]{config}));
            }
        } else if ((LackSocketHandler.z.contains(session)) && (config.getLackBaodanStatus() != -1)) {
            int status = state ? 1 : 0;
            config.setLackBaodanStatus(status);
            this.systemConfigServiceDao.updateLackBaodanStatus(status);
        }
        for (Map.Entry entry : GameParameterAssist.be.entrySet()) {
            AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity((IoSession) entry.getValue(), "syncConfig", new Object[]{config}));
        }
    }

    public void deskLock(int deskId, IoSession session) {
        if (CardSocketHandler.w.contains(session)) {
            CardDesk c = (CardDesk) cardDeskServiceDao.getById(deskId);
            if (c.getId() == deskId) {
                c.setState(0);
                cardDeskServiceDao.updateStateById(deskId, 0);
                for (IoSession s : CardSocketHandler.w) {
                    AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(s, "manageService/updateDesk", new Object[]{c}));
                }
            }
        } else if (LuckSocketHandler.A.contains(session)) {
            List<Desk> desks = this.deskServiceDao.getAllDesk();
            for (Desk d : desks) {
                if (d.getId() == deskId) {
                    d.setState(0);
                    this.deskServiceDao.updateStateById(deskId, 0);
                    for (IoSession s : com.miracle9.game.server.message.LuckSocketHandler.A) {
                        AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(s, "manageService/updateDesk", new Object[]{d}));
                    }
                    break;
                }
            }
        } else if (FishKingSocketHandler.x.contains(session)) {
            List<FishKingDesk> fishDesks = this.fishKingDB.getFishKingDeskList();
            for (FishKingDesk fd : fishDesks) {
                if (fd.getId() == deskId) {
                    fd.setState(0);
                    this.fishKingDB.updateStateById(deskId, 0);
                    for (IoSession s : FishKingSocketHandler.x) {
                        AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(s, "manageService/updateDesk", new Object[]{fd}));
                    }
                    break;
                }
            }
        } else if (FishSocketHandler.x.contains(session)) {
            List<FishDesk> fishDesks = this.fishDeskServiceDao.getFishDesks();
            for (FishDesk fd : fishDesks) {
                if (fd.getId() == deskId) {
                    fd.setState(0);
                    this.fishDeskServiceDao.updateStateById(deskId, 0);
                    for (IoSession s : FishSocketHandler.x) {
                        AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(s, "manageService/updateDesk", new Object[]{fd}));
                    }
                    break;
                }
            }
        } else if (com.miracle9.game.server.message.BulletSocketHandler.v.contains(session)) {
            List<BulletFishDesk> bulletFishDesks = this.bulletFishDeskServiceDao.gteBulletFishDesks();
            for (BulletFishDesk bfd : bulletFishDesks) {
                if (bfd.getId() == deskId) {
                    bfd.setState(0);
                    this.bulletFishDeskServiceDao.updateStateById(deskId, 0);
                    for (IoSession s : com.miracle9.game.server.message.BulletSocketHandler.v) {
                        AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(s, "manageService/updateDesk", new Object[]{bfd}));
                    }
                    break;
                }
            }
        } else if (MermaidSocketHandler.B.contains(session)) {
            List<MermaidDesk> mermaidDesks = this.mermaidDeskServiceDao.getMermaidDesks();
            for (MermaidDesk md : mermaidDesks) {
                if (md.getId() == deskId) {
                    md.setState(0);
                    this.mermaidDeskServiceDao.updateStateById(deskId, 0);
                    for (IoSession s : MermaidSocketHandler.B) {
                        AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(s, "manageService/updateDesk", new Object[]{md}));
                    }
                    break;
                }
            }
        } else if (LackSocketHandler.z.contains(session)) {
            List<LackDesk> lackDesks = this.lackDeskServiceDao.getLackDesks();
            for (LackDesk lackDesk : lackDesks) {
                if (lackDesk.getId() == deskId) {
                    lackDesk.setState(0);
                    this.lackDeskServiceDao.updateStateById(deskId, 0);
                    for (IoSession s : LackSocketHandler.z) {
                        AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(s, "manageService/updateDesk", new Object[]{lackDesk}));
                    }
                    break;
                }
            }
        } else if (com.miracle9.game.server.message.JoySocketHandler.x.contains(session)) {
            List<JoyDesk> joyDesks = this.joyDeskServiceDao.getJoyDesks();
            for (JoyDesk joyDesk : joyDesks) {
                if (joyDesk.getId() == deskId) {
                    joyDesk.setState(0);
                    this.joyDeskServiceDao.updateStateByById(deskId, 0);
                    for (IoSession s : com.miracle9.game.server.message.JoySocketHandler.x) {
                        AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(s, "manageService/updateDesk", new Object[]{joyDesk}));
                    }
                    break;
                }
            }
        } else if (WaterSocketHandler.F.contains(session)) {
            List<WaterDesk> waterDesks = this.waterDeskServiceDao.getWaterDesks();
            for (WaterDesk waterDesk : waterDesks) {
                if (waterDesk.getId() == deskId) {
                    waterDesk.setState(0);
                    this.waterDeskServiceDao.updateStateById(deskId, 0);
                    for (IoSession s : WaterSocketHandler.F) {
                        AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(s, "manageService/updateDesk", new Object[]{waterDesk}));
                    }
                    break;
                }
            }
        }
    }

    public void dingFen(int nUserId, IoSession session) {
        IoSession s = GameParameterAssist.ba.get(nUserId);
        if (s != null) {
            AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(s, "dingFen", new Object[0]));
        }
    }

    public void lastGameScore(int nUserId, int nScore, IoSession session) {
        IoSession s = GameParameterAssist.ba.get(nUserId);
        if (s == null) {
            return;
        }
        User user = (User) this.userServiceDao.getById(nUserId);
        if (user.getLastGame() == 6) {
            AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(s, "newGameGold", new Object[]{Integer.valueOf(user.getGameGold())}));
            AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(s, "newExpeGold", new Object[]{Integer.valueOf(user.getExpeGold())}));
            AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(s, "lastGameScore", new Object[]{Integer.valueOf(nScore)}));
        } else {
            AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(s, "lastGameScore", new Object[]{Integer.valueOf(nScore)}));
        }
    }

    public void baoDanCount(int nCount, IoSession session) {
        Map<String, Object> result = new HashMap<>();
        if (CardSocketHandler.w.contains(session)) {
            GameParameterAssist.aM.put(1, nCount);
            for (Map.Entry<Integer, IoSession> entry : GameParameterAssist.be.entrySet()) {
                result.put("count", nCount);
                result.put("baodanType", 1);
                AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(entry.getValue(), "baodanCountNew", new Object[]{result}));
            }
        } else if (LackSocketHandler.z.contains(session)) {
            GameParameterAssist.aM.put(2, nCount);
            for (Map.Entry<Integer, IoSession> entry : GameParameterAssist.be.entrySet()) {
                result.put("count", nCount);
                result.put("baodanType", 2);
                AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(entry.getValue(), "baodanCountNew", new Object[]{result}));
            }
        }
    }

    public static void sendNotice(String content) {
        for (Integer uid : GameParameterAssist.ba.keySet()) {
            IoSession s = GameParameterAssist.ba.get(uid);
            Map<String, Object> result = new HashMap<>();
            result.put("content", content);
            MessageSend.q.notifyAllOperating(new StorageMethodEntity(s, "scrollMessage", new Object[]{result}));
        }
    }

    private String lllllllllIlllIlll(int type) {
        Map<Integer, String> award = new HashMap<>();
        award.put(Integer.valueOf(1), "大刀全盘奖150倍");
        award.put(Integer.valueOf(2), "长枪全盘奖100倍");
        award.put(Integer.valueOf(3), "斧头全盘奖50倍");
        award.put(Integer.valueOf(4), "林冲全盘奖400倍");
        award.put(Integer.valueOf(5), "宋江全盘奖500倍");
        award.put(Integer.valueOf(6), "鲁智深全盘奖250倍");
        award.put(Integer.valueOf(7), "忠义堂全盘奖2500倍");
        award.put(Integer.valueOf(8), "替天行道全盘奖1000倍");
        award.put(Integer.valueOf(9), "龙头全盘奖5000倍");
        award.put(Integer.valueOf(10), "武器全盘奖15倍");
        award.put(Integer.valueOf(11), "人物全盘奖50倍");
        return award.get(Integer.valueOf(type));
    }

    public void waterMarginAwardBroadcast(int deskId, int type, IoSession session) {
        WaterDesk wd = this.waterDeskServiceDao.getWaterDeskByDeskId(deskId);
        String deskName = wd.getName();
        String award = lllllllllIlllIlll(type);
        String content = String.format("恭喜竞技厅%s桌赢得%s! 大家赶快来参加吧!", deskName, award);
        sendNotice(content);
        for (IoSession ioSession : WaterSocketHandler.F) {
            AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity(ioSession, "manageService/notice", new Object[]{content}));
        }
    }

    public void addPrivateDesk(JoyDesk joyDesk, IoSession session) {
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (joyDesk.getModeId() == 2) {
            this.joyDeskServiceDao.voidMethod();
            JoyDesk[] arrJoyDesk = this.deskService.getJoyDeskList();
            for (Map.Entry entry : GameParameterAssist.be.entrySet()) {
                AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity((IoSession) entry.getValue(), "getJoyDeskList", new Object[]{arrJoyDesk}));
            }
        }
    }

    public void deletePriJoyDesk(JoyDesk joyDesk, IoSession session) {
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.deskService.recordDeleteDesk(GameTypeEnums.GT_6.getType(), joyDesk.getId(), joyDesk.getExchange());
        if (joyDesk.getModeId() == 2) {
            this.joyDeskServiceDao.voidMethod();
            JoyDesk[] arrJoyDesk = this.deskService.getJoyDeskList();
            for (Map.Entry<Integer, IoSession> entry : GameParameterAssist.be.entrySet()) {
                AmfMessageSend.storageMethodOperating.notifyAllOperating(new StorageMethodEntity((IoSession) entry.getValue(), "getJoyDeskList", new Object[]{arrJoyDesk}));
            }
        }
    }

    public void singleReward(int nUserId, int nDeGold, int nActivityId, IoSession session) {
        ActivityInfo activityInfo = this.activityInfoServiceDao.getActivityInfo();
        if (activityInfo != null) {
            this.activityService.checkSingleUserActivity(nUserId, nDeGold, nActivityId);
        }
    }

    public void currentJackpotScore(int nDeskId, int nScore, IoSession session) {
        GameParameterAssist.J.put(Integer.valueOf(nDeskId), Integer.valueOf(nScore));
    }

    public void requestSeat(int userId, int type, IoSession session) {
        synchronized (this) {
            log.info("recive request seat from game:" + getGameName(type) + ",userId:" + userId);
            int status = UserParticipateRecord.checkUserId_2(userId);
            log.info("status:" + status + ",userId:" + userId);
            if (status == -2) {
                log.info("userId:" + userId + ",seatStatus not in seat change to " + type);
                UserParticipateRecord.addUserStatusMapCQ(userId, type);
                return;
            }
            if (status != type) {
                log.info("something going wrong status:" + status + ",type:" + type + ",userId:" + userId);
                this.userServiceDao.updateStatus(userId, 3);
                User user = (User) this.userServiceDao.getById(userId);
                AdminLog adminLog = new AdminLog("系统防刷分管理员", "自动对会员“" + user.getUsername() + "”进行了“封号”操作" + ",原因：在不同的小游戏中请求座位", 0, 0, "Frozen the member which ID is " + user.getUsername());
                this.adminLogDao.addAdminLog(adminLog);
                gameKick(status - 8, userId);
                gameKick(type - 8, userId);
                log.info("kick two success");
                UserService.quitToLogin(userId, 2);
                log.info("kick and freez success");
                log.info("user cheat userId:" + userId + ",status:" + 3);
                log.error("user cheat userId:" + userId + ",status:" + 3);
                com.miracle9.game.thread.DealCheatThread.f(userId);
            }
        }
    }

    public void requestDesk(int userId, int deskId, IoSession session) {
        log.info("recive request desk deskId:" + deskId + ",userId:" + userId);
        boolean can = UserParticipateRecord.deskRecord(userId, deskId);
        if (!can) {
            log.info("userId:" + userId + ",frequecy request desk:" + deskId);
            UserService.quitToLogin(userId, 2);
            this.userServiceDao.updateStatus(userId, 2);
        }
    }

    public void reLogin(int userId, int gameType, IoSession session) {
        if (!GameParameterAssist.aB) {
            return;
        }
        if (UserParticipateRecord.checkUserId_4(userId) != -4) {
            gameKick(gameType, userId);
        } else {
            if (GameParameterAssist.aB) {
                User user = (User) this.userServiceDao.getById(userId);
                if (!user.getUsername().startsWith("robot-")) {
                    if ((gameType == GameTypeEnums.GT_0.getType()) && (GameParameterAssist.bh.containsKey(userId))) {
                        return;
                    }
                    if ((gameType == GameTypeEnums.GT_1.getType()) && (GameParameterAssist.bi.containsKey(userId))) {
                        return;
                    }
                    if ((gameType == GameTypeEnums.GT_2.getType()) && (GameParameterAssist.bj.containsKey(userId))) {
                        return;
                    }
                    if ((gameType == GameTypeEnums.GT_3.getType()) && (GameParameterAssist.bk.containsKey(userId))) {
                        return;
                    }
                    if ((gameType == GameTypeEnums.GT_4.getType()) && (GameParameterAssist.bl.containsKey(userId))) {
                        return;
                    }
                    if ((gameType == GameTypeEnums.GT_5.getType()) && (GameParameterAssist.aN.containsKey(userId))) {
                        return;
                    }
                    if ((gameType == GameTypeEnums.GT_7.getType()) && (GameParameterAssist.bs.containsKey(userId))) {
                        return;
                    }
                    if ((gameType == GameTypeEnums.GT_9.getType()) && (GameParameterAssist.thousandFishGametypeMap.containsKey(userId))) {
                        return;
                    }
                    if ((gameType == GameTypeEnums.GT_11.getType()) && (GameParameterAssist.fishKingBi.containsKey(userId))) {
                        return;
                    }
                    if ((gameType == GameTypeEnums.GT_20.getType()) && (GameParameterAssist.likuiFishUserId.containsKey(userId))) {
                        return;
                    }
                    if ((gameType == GameTypeEnums.GT_37.getType()) && (GameParameterAssist.goldenFishUserId.containsKey(userId))) {
                        return;
                    }
                    if ((gameType == GameTypeEnums.GT_42.getType()) && (GameParameterAssist.goldSharkUserId.containsKey(userId))) {
                        return;
                    }
                    if ((gameType == GameTypeEnums.GT_52.getType()) && (GameParameterAssist.bcbmUserId.containsKey(userId))) {
                        return;
                    }
                    if ((gameType == GameTypeEnums.GT_92.getType()) && (GameParameterAssist.tgUserId.containsKey(userId))) {
                        return;
                    }
                    if ((gameType == GameTypeEnums.GT_103.getType()) && (GameParameterAssist.lhdUserId.containsKey(userId))) {
                        return;
                    }
                }
            }
            if (gameType == GameTypeEnums.GT_0.getType()) {
                GameParameterAssist.bh.put(userId, gameType);
            } else if (gameType == GameTypeEnums.GT_1.getType()) {
                GameParameterAssist.bi.put(userId, gameType);
            } else if (gameType == GameTypeEnums.GT_2.getType()) {
                GameParameterAssist.bj.put(userId, gameType);
            } else if (gameType == GameTypeEnums.GT_3.getType()) {
                GameParameterAssist.bk.put(userId, gameType);
            } else if (gameType == GameTypeEnums.GT_4.getType()) {
                GameParameterAssist.bl.put(userId, gameType);
            } else if (gameType == GameTypeEnums.GT_5.getType()) {
                GameParameterAssist.aN.put(userId, gameType);
            } else if (gameType == GameTypeEnums.GT_6.getType()) {
                GameParameterAssist.aP.put(userId, gameType);
            } else if (gameType == GameTypeEnums.GT_7.getType()) {
                GameParameterAssist.bs.put(userId, gameType);
            } else if (gameType == GameTypeEnums.GT_9.getType()) {
                GameParameterAssist.thousandFishGametypeMap.put(userId, gameType);
            } else if (gameType == GameTypeEnums.GT_11.getType()) {
                GameParameterAssist.fishKingBi.put(userId, gameType);
            } else if (gameType == GameTypeEnums.GT_20.getType()) {
                GameParameterAssist.likuiFishUserId.put(userId, gameType);
            } else if (gameType == GameTypeEnums.GT_37.getType()) {
                GameParameterAssist.goldenFishUserId.put(userId, gameType);
            } else if (gameType == GameTypeEnums.GT_42.getType()) {
                GameParameterAssist.goldSharkUserId.put(userId, gameType);
            }  else if (gameType == GameTypeEnums.GT_52.getType()) {
                GameParameterAssist.bcbmUserId.put(userId, gameType);
            } else if (gameType == GameTypeEnums.GT_92.getType()) {
                GameParameterAssist.tgUserId.put(userId, gameType);
            } else if (gameType == GameTypeEnums.GT_103.getType()) {
                GameParameterAssist.lhdUserId.put(userId, gameType);
            }
            synchronized (this) {
                int loginStatus = UserParticipateRecord.checkUserId_3(userId);
                if (loginStatus == -3) {
                    UserParticipateRecord.addUserStatusMapCP(userId, gameType);
                } else if (loginStatus != gameType) {
                    this.userServiceDao.updateStatus(userId, 3);
                    User user2 = (User) this.userServiceDao.getById(userId);
                    AdminLog adminLog = new AdminLog("系统防刷分管理员", "自动对会员“" + user2.getUsername() + "”进行了“封号”操作" + ",原因：在不同的小游戏同时登录", 0, 0, "Frozen the member which ID is " + user2.getUsername());
                    this.adminLogDao.addAdminLog(adminLog);
                    gameKick(loginStatus, userId);
                    gameKick(gameType, userId);
                    UserService.quitToLogin(userId, 2);
                    com.miracle9.game.thread.DealCheatThread.f(userId);
                }
            }
        }
    }

}
